package com.gangwangchina.android.contactlib.test;

import java.util.Iterator;

import com.gangwangchina.android.contactlib.ContactActivity;
import com.gangwangchina.android.contactlib.ContactsHelper;
import com.gangwangchina.android.contactlib.exceptions.NoIdException;
import com.gangwangchina.android.contactlib.model.PersonContact;
import com.gangwangchina.android.contactlib.model.PersonName;
import com.gangwangchina.android.contactlib.model.TelephoneNumber;

import android.content.Context;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.test.ActivityInstrumentationTestCase2;

public class testCases extends
		ActivityInstrumentationTestCase2<ContactActivity> {

	private Context mCtx = null;

	private ContactsHelper mContactsHelper = null;

	public testCases(/*Class<ContactActivity> activityClass*/) {
//		super(activityClass);
		super("com.gangwangchina.android.contactlib", ContactActivity.class);
		// TODO Auto-generated constructor stub

	}

	protected void setUp() {
		try {
			super.setUp();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (mCtx == null)
			mCtx = this.getActivity();

		this.assertTrue(mCtx != null);

		mContactsHelper = new ContactsHelper(this.mCtx);

		deleteAllContacts();
	}

	protected void tearDown() {
		try {
			super.tearDown();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void deleteAllContacts() {

		Iterator<PersonContact> pcs = mContactsHelper.listContacts();

		while (pcs!= null && pcs.hasNext()) {
			PersonContact p = pcs.next();

			if(p == null)
			{
				break;
			}
			
			try {
				mContactsHelper.delete(p);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OperationApplicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
	public void testAddPersonContact() throws NoIdException, RemoteException,
			OperationApplicationException {
		PersonContact one = PersonContact
				.newPersonContact(null);
		
		PersonName name = PersonName.newPersonName("FirstName", "LastName",
				null, null, null);
		TelephoneNumber tel = TelephoneNumber.newTelephoneNumber("123123123",
				3, null);
		TelephoneNumber tel2 = TelephoneNumber.newTelephoneNumber("1231231230",
				1, null);

		one.name = name;
		one.addTelephoneNumber(tel);
		one.addTelephoneNumber(tel2);

		mContactsHelper.add(one);
		
		PersonContact pc = mContactsHelper.getPersonContactById(one.id);
		pc.fill(mCtx);
		
		this.assertEquals("first name must be \"FirstName\"", one.name.familyName, pc.name.familyName);
		this.assertEquals("last name must be \"LastName\"", one.name.givenName, pc.name.givenName);
		
		this.assertEquals("telephoneNumbers count must be: 2",one.telephoneNumbers.size(), pc.telephoneNumbers.size());
		this.assertEquals("first telephone number must be: \"123123123\"",one.telephoneNumbers.get(0).number, pc.telephoneNumbers.get(0).number);
		this.assertEquals("next telephone number must be: \"1231231230\"",one.telephoneNumbers.get(1).number, pc.telephoneNumbers.get(1).number);

	}
}
